package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.Video;
import com.drcoffee.drcoffeemall.mapper.VideoMapper;
import com.drcoffee.drcoffeemall.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class VideoServiceImpl implements VideoService {

    final
    VideoMapper videoMapper;

    public VideoServiceImpl(VideoMapper videoMapper) {
        this.videoMapper = videoMapper;
    }

    @Override
    public Map<String, Object> getVideo(Integer machine_id, String search) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Video> videoList;
            if (machine_id == null) {
                videoList = videoMapper.getVideoBySearch('%' + search + '%');
            } else {
                videoList = videoMapper.getVideo(machine_id, '%' + search + '%');
            }
            resultMap.put("videoList", videoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getVideoAll() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Video> videoList = videoMapper.getVideoAll();
            resultMap.put("videoList", videoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> addVideo(Video video) {
        Map<String, Object> resultMap = new HashMap<>();
//        video.setVideo_url(data_img_init +"/image"+ video.getVideo_url().replaceAll("\\\\", "/"));
        boolean result = videoMapper.addVideo(video);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "添加成功");
        } else {
            resultMap.put("message", "添加失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> updateVideo(Video video) {
        Map<String, Object> resultMap = new HashMap<>();
//        video.setCover_url(data_img_init + '/' + video.getCover_url().replaceAll("\\\\", "/"));
        boolean result = videoMapper.updateVideo(video);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "修改成功");
        } else {
            resultMap.put("message", "修改失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> updateVideoText(Video video) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = videoMapper.updateVideoText(video);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "修改成功");
        } else {
            resultMap.put("message", "修改失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> deleteOld(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        Video video = videoMapper.getVideoById(id);

        try {
//            if (!Objects.equals(video.getCover_url(), "") && video.getCover_url() != null) {
//                boolean delResult = FileUploadDownloadUtils.deleteFile(video.getCover_url());
//                if (delResult) {
//                    resultMap.put("result", true);
//                } else {
//                    resultMap.put("result", false);
//                }
//            } else {
//                resultMap.put("result", true);
//            }
        } catch (Exception e) {
            resultMap.put("error", e.toString());
            e.printStackTrace();
        }
        return resultMap;
    }
}
